import { HashUtil } from '../../util/HashUtil';
import { BaseRequest } from "@hw-agconnect/credential-ohos";

export class FunctionRequest extends BaseRequest {
  private REQUEST_URL: string = "/agc/apigw/wisefunction/functions";
  private readonly body: any;
  private httpTriggerURI: string;
  private authorization: string = '';

  constructor(name: string, httpTriggerURI: string, body: any) {
    super(name);
    this.setSdkServiceName("agconnect-function");
    this.body = body;
    this.httpTriggerURI = httpTriggerURI;
  }

  async getBody(): Promise<any> {
    return Promise.resolve(this.body);
  }

  async getHeader(): Promise<any> {
    let header = await super.getHeader();
    header.Authorization = this.authorization;
    header.accessToken = this.accessToken;
    return Promise.resolve(header);
  }

  async getUrl(): Promise<string> {
    let teamId: string = await this.getCpId();
    let productId: string = await this.getProductId();
    let appId: string = HashUtil.hash(teamId, productId);
    if (this.httpTriggerURI && this.httpTriggerURI.length > 0 && this.httpTriggerURI.charAt(0) != '/') {
      this.httpTriggerURI = '/' + this.httpTriggerURI;
    }
    return Promise.resolve(await super.getUrl() + this.REQUEST_URL + "/" + appId + this.httpTriggerURI);
  }

  setAuthorization(authorization: string) {
    this.authorization = authorization;
  }

  setAccessToken(token: string) {
    this.accessToken = token;
  }
}